package com.schoolhelper.controller.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.schoolhelper.controller.activity.R;
import com.schoolhelper.model.adaptermodel.MainRecyclerViewSelectLine;

import java.util.List;

public class MainRecyclerViewSelectLineAdapter extends RecyclerView.Adapter<MainRecyclerViewSelectLineAdapter.ViewHolder> implements View.OnClickListener {

    private int selectType = 1;

    private List<MainRecyclerViewSelectLine> selectLineList;

    // 写一个点击监听器接口
    private OnItemClickListener itemClickListener;

    // 构造函数，参数设为自己需要的类型
    public MainRecyclerViewSelectLineAdapter(List<MainRecyclerViewSelectLine> mainRecyclerViewSelectLines){
        selectLineList = mainRecyclerViewSelectLines;

    }

    public int getSelectType() {
        return selectType;
    }

    // 重写viewHolder，设置holder，绑定布局
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView;

        public ViewHolder(View view){
            super(view);
            textView = (TextView) view.findViewById(R.id.main_select_tv);
        }
    }

    // 设置点击操作
    @Override
    public void onClick(View v) {
        if (itemClickListener != null){
            itemClickListener.onItemClick((Integer) v.getTag(), selectType);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        // 加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_selectline_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);// 实例化holder

        view.setOnClickListener(this);// 设置点击监听事件
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        // holder对布局对象进行操作
        MainRecyclerViewSelectLine selectLine = selectLineList.get(position);
        holder.textView.setText(selectLine.getText());
        holder.itemView.setTag(position);
    }


    @Override
    public int getItemCount() {
        return selectLineList.size();
    }

    public void setItemClickListener(OnItemClickListener onitemClickListener , int type){
        itemClickListener = onitemClickListener;
    }


}


